<?php

namespace app\model\task;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-task
 * Class TaskModel
 * @package app\model
 */
class UserTaskModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'user_task';

    /**
     * 范围查询-status
     * @param $query
     * @param $status
     */
    public function scopeStatus($query, $status)
    {
        if ($status != null) {
            $query->where('task_status', '=', $status);
        }
    }


    /**
     * 范围查询-keyword
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('task_title', '%' . trim($keyword) . '%');
        }
    }

    /**
     * 获取分页列表
     * @param $status
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @return array
     */
    public function getPageData($status, $keyword, $current_page, $list_rows, $field = '*')
    {
        $dataList = $this
            ->field($field)
            ->status($status)
            ->keyword($keyword)
            ->order('create_time', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * 参与任务
     * @param $formData
     * @param $userInfo
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function joinTask($formData, $userInfo)
    {
        $tid = $formData['tid'];
        if (isset($userInfo['user_id'])) {
            $TaskModel = new TaskModel();
            $task_data = $TaskModel->where(['id' => $tid, 'task_status' => 1])->field('id,task_price')->find();
            if (empty($task_data)) {
                $rsps['code'] = 0;
                $rsps['msg'] = "任务已下线";
                return $rsps;
            }
            $task_info = [
                'task_id' => $task_data['id'],
                'user_id' => $userInfo['user_id'],
                'price' => $task_data['task_price'],
                'integral' => 0,
            ];
            $res = $this->save($task_info);
            if ($res === false) {
                $rsps['code'] = 0;
                $rsps['msg'] = "添加失败";
                $rsps['data'] = [];
                return $rsps;
            }
            $rsps['code'] = 1;
            $rsps['msg'] = "加入成功";
            $rsps['data'] = [];
            return $rsps;
        }
        $rsps['code'] = 2006;
        $rsps['msg'] = "用户信息有误";
        $rsps['data'] = [];
        return $rsps;
    }

    /**
     * 任务结束并且结算任务奖励
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function endTask($id)
    {
        if (!empty($id)) {

            $ret = $this->where('id', $id)->update(['status' => 3]);
            if ($ret === true) {

                //奖励结算规则待定

                $reps['code'] = 1;
                $rsps['msg'] = "任务结束成功";
                $rsps['data'] = [];
                return $reps;
            }
            $reps['code'] = 0;
            $rsps['msg'] = "任务结束失败";
            $rsps['data'] = [];
            return $reps;
        }
        $reps['code'] = 991;
        $rsps['msg'] = "参数有误";
        $rsps['data'] = [];
        return $reps;
    }
}
